import request from "@/utils/request";

const state = {
  today: {},
};

const getters = {
  today: (state) => state.today,
};

const actions = {
  async today({ state }) {
    const location = uni.getStorageSync("location") || "suzhou";
    const weather = await request({
      url: "/api/v3/weather/now.json",
      // baseURL: process.env.VUE_APP_BASE_API,
      data: {
        location,
      },
    });
    const air = await request({
      url: "/api/v3/air/now.json",
      // baseURL: process.env.VUE_APP_BASE_API,
      data: {
        location,
      },
    });
    try {
      const data = {
        ...weather.results[0].now,
        ...weather.results[0].location,
        ...air.results[0].air.city,
      };
      console.log(JSON.parse(JSON.stringify(data)));
      state.today = data;
      return data;
    } catch {
      return {};
    }
  },
};

export default {
  namespaced: true,
  state,
  actions,
  getters,
};
